package com.example.myapplication4;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication4.adapter.GridViewAdapter;

import com.example.myapplication4.bean.Student;

import java.util.ArrayList;
import java.util.List;

public class GridViewActivity extends AppCompatActivity {
    private GridView mGridView;
    private List<Student> mList;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        mGridView = findViewById(R.id.gridview);
        initData();
        GridViewAdapter adapter = new GridViewAdapter(this, mList);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Student student = (Student) parent.getItemAtPosition(position);
                Log.e("test123", "=====================" + student);
            }
        });
    }

    private void initData() {
        mList = new ArrayList<>();
        Student student;
        for (int i = 0; i < 31; i++) {
            student = new Student();
            student.setName("张小" + i);
            student.setNumber("00000000" + i);
            mList.add(student);
        }
    }
}